package com.divit.springboot.application.util;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

@Component
public class ServerInitializer implements ApplicationRunner {

  @Override
  public void run(ApplicationArguments args) throws Exception {
    System.out.println("started");
    DatagramSocket receiveSocket = new DatagramSocket(63002);
    byte[] receiveData = new byte[1024];
    while (true) {
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length, InetAddress.getByName("10.13.154.17"), 63002);
      receiveSocket.receive(receivePacket);
      String received = new String(receivePacket.getData(), 0, receivePacket.getLength());
      receiveSocket.close();
      System.out.println(received);
    }
  }
}
